package com.itboye.jigongbao

import android.graphics.drawable.Drawable
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.view.View
import android.widget.TextView
import com.itboye.jigongbao.app.MyApplication
import com.itboye.jigongbao.base.BaseActivity
import com.itboye.jigongbao.bean.LoginInfo
import com.itboye.jigongbao.fragments.AccountBookFragment
import com.itboye.jigongbao.fragments.IndexFragment
import com.itboye.jigongbao.fragments.MeFragment
import com.itboye.jigongbao.utils.Const

class MainActivity : BaseActivity() {

    var btn_index: TextView? = null
    var btn_second: TextView? = null
    var btn_third: TextView? = null
    var drawableTop: Drawable? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initData() {
        if (MyApplication.instance!!.mACache!!.getAsObject(Const.LOGIN_INFO)!=null) {
            MyApplication.instance!!.loginInfo = MyApplication.instance!!.mACache!!.getAsObject(Const.LOGIN_INFO) as LoginInfo
//            ByAlert.alert(MyApplication.instance!!.loginInfo!!.getUid())
        }
        fragmentManager = getSupportFragmentManager()
        setSelectTab(0)
    }

    override fun onClick(p0: View?) {
        when (p0) {
            btn_index -> {
                selectIndex = 0
            }
            btn_second -> {
                selectIndex = 1
            }
            btn_third -> {
                selectIndex = 2
            }
        }
        setSelectTab(selectIndex)
    }

    internal var indexFragment: IndexFragment? = null
    internal var accountBookFragment: AccountBookFragment? = null
    internal var meFragment: MeFragment? = null
    var selectIndex = 0
    private var fragmentManager: FragmentManager? = null

    private fun setSelectTab(selectIndex: Int) {
        // 每次选中之前先清楚掉上次的选中状态
        // 开启一个Fragment事务
        val transaction = fragmentManager!!.beginTransaction()
        //清除按钮选中状态
        clearSelectStataus()
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction)
        when (selectIndex) {
            0//首页
            -> {
                btn_index!!.setTextColor(resources.getColor(R.color.main_blue))
                btn_index!!.setCompoundDrawablesWithIntrinsicBounds(null, getDrawableTop(R.drawable.shouyexuanzhong), null, null)
                if (indexFragment == null) {
                    //如果HomeFragment为空，则创建一个添加到界面
                    indexFragment = IndexFragment(selectIndex)
                    transaction.add(R.id.framelayout, indexFragment)
                } else {
                    // 如果HomeFragment不为空，则直接将它显示出来
                    transaction.show(indexFragment)
                }
            }

            1//
            -> {
                btn_second!!.setTextColor(resources.getColor(R.color.main_blue))
                btn_second!!.setCompoundDrawablesWithIntrinsicBounds(null, getDrawableTop(R.drawable.zhangbenxuanzhong), null, null)
                if (accountBookFragment == null) {
                    //如果HomeFragment为空，则创建一个添加到界面
                    accountBookFragment = AccountBookFragment(selectIndex)
                    transaction.add(R.id.framelayout, accountBookFragment)
                } else {
                    // 如果HomeFragment不为空，则直接将它显示出来
                    transaction.show(accountBookFragment)
                }
            }
            2//
            -> {
                btn_third!!.setTextColor(resources.getColor(R.color.main_blue))
                btn_third!!.setCompoundDrawablesWithIntrinsicBounds(null, getDrawableTop(R.drawable.wodehuahzong), null, null)
                if (meFragment == null) {
                    //如果HomeFragment为空，则创建一个添加到界面
                    meFragment = MeFragment(selectIndex)
                    transaction.add(R.id.framelayout, meFragment)
                } else {
                    // 如果HomeFragment不为空，则直接将它显示出来
                    transaction.show(meFragment)
                }
            }
        }
        transaction.commit()
    }

    private fun clearSelectStataus() {
        //文字
        btn_index!!.setTextColor(resources.getColor(R.color.gray_6A))
        btn_second!!.setTextColor(resources.getColor(R.color.gray_6A))
        btn_third!!.setTextColor(resources.getColor(R.color.gray_6A))
        //drawableTop
        var drawableTop = getDrawableTop(R.drawable.shouye)
        btn_index!!.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop, null, null)
        drawableTop = getDrawableTop(R.drawable.zhangben)
        btn_second!!.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop, null, null)
        drawableTop = getDrawableTop(R.drawable.wode)
        btn_third!!.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop, null, null)
    }

    fun getDrawableTop(rs: Int): Drawable {
        drawableTop = resources.getDrawable(rs)
//        drawableTop!!.setBounds(0, drawableTop!!.minimumWidth, 0, drawableTop!!.minimumHeight)
        return drawableTop!!
    }

    private fun hideFragments(transaction: FragmentTransaction?) {
        if (indexFragment != null) {
            if (transaction != null) {
                transaction.hide(indexFragment)
            }
        }
        if (accountBookFragment != null) {
            if (transaction != null) {
                transaction.hide(accountBookFragment)
            }
        }
        if (meFragment != null) {
            if (transaction != null) {
                transaction.hide(meFragment)
            }
        }
    }
}
